package com.aisino.cvasp.application.controller;

import com.aisino.cvasp.application.domain.Education;
import com.aisino.cvasp.application.domain.FormData;
import com.aisino.cvasp.application.service.CreateForm;
import com.aisino.cvasp.application.service.CreateParagraph;
import com.aisino.cvasp.application.service.CreatePhoto;
import com.aisino.cvasp.application.service.CreateTable;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import org.springframework.core.io.ClassPathResource;

import java.io.*;


public class DocTestController {


    public static  String PDF_NEW2_PATH = "D:\\privateWorkSpace\\my-itext5-pdf-master\\my-itext5-pdf-master\\src\\main\\resources\\template\\new2.pdf";

    public static void main(String[] args) throws IOException, DocumentException {

        // TODO:   3 读取模板

        org.springframework.core.io.Resource resource = new ClassPathResource("template/new.pdf");
        InputStream inputStream = resource.getInputStream();

        FileOutputStream fos = new FileOutputStream(PDF_NEW2_PATH);

        // TODO:   4 读取模板表单进行追加
        // 若要在 form表单落地前继续操作 要使用 PdfReader  reader = new PdfReader(bos.toByteArray());
        PdfReader reader = new PdfReader(inputStream);
        int pageNum = reader.getNumberOfPages(); //当前的页码
        Rectangle onePage = reader.getPageSize(1);
        Document document = new Document(onePage);

        // TODO:   4 new一个新表格
        //Document document = new Document(PageSize.A4);


        PdfWriter pdfWriter = PdfWriter.getInstance(document, fos);
        //PDF版本(默认1.4)
        pdfWriter.setPdfVersion(PdfWriter.PDF_VERSION_1_4);
        // TODO:   5 打开文档。
        document.open();

        // TODO:   6 原pdf继续操作
        PdfContentByte cbUnder = pdfWriter.getDirectContentUnder();
        for (int i = 1; i <= pageNum; i++) {
            PdfImportedPage pageTemplate = pdfWriter.getImportedPage(reader, i);
            cbUnder.addTemplate(pageTemplate, 0, 0);
            //重新开一页面
            document.newPage();
        }

        // 定义通用字体
        BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);


        // TODO:   7 向文档中添加段落标题。
        CreateParagraph.createParagraph(document,baseFont);


        // TODO:   8 向文档中添加表格
        CreateTable.createTable(document, baseFont);
        // TODO:   7 向文档中添加表格
        CreateParagraph.createTable(document,baseFont);

        document.newPage(); //新增一页
        // TODO:   9 向文档中添加图片
        CreatePhoto.createPhoto2(document);


        // TODO:   8 向文档中添加图片
        CreateParagraph.createImage(document,baseFont);

        // TODO:   9 关闭文档,会将pdfWriter 输出。
        document.close();
        reader.close();
        inputStream.close();

    //
    }





}
